{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'docs':[\n",
    "        'my dog has flea problems help please',\n",
    "        'maybe not take him to dog park stupid',\n",
    "        'my dalmation is so cute I love him',\n",
    "        'stop posting stupid worthless grabage',\n",
    "        'mr licks ate mt steak how to stop him',\n",
    "        'quit buying worthless dog food stupid',\n",
    "        \n",
    "    ],\n",
    "    'labels':[0,1,0,1,0,1]\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],\n",
       " ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],\n",
       " ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],\n",
       " ['stop', 'posting', 'stupid', 'worthless', 'grabage'],\n",
       " ['mr', 'licks', 'ate', 'mt', 'steak', 'how', 'to', 'stop', 'him'],\n",
       " ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['docs'] = list(map(lambda doc:doc.split(' '),data['docs']))\n",
    "data['docs']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getDocList(docs):\n",
    "    docSet = set([])\n",
    "    for doc in docs:\n",
    "        docSet = set(doc) | docSet\n",
    "    doclist = list(docSet)\n",
    "    doclist.sort()\n",
    "    return doclist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "docList = getDocList(data['docs'])\n",
    "docList.index('I')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def doc2V(doc,doclist):\n",
    "    #单词集合的维度\n",
    "    dims = len(doclist)\n",
    "    #初始化 坐标点\n",
    "    doc_v= [0]*dims\n",
    "\n",
    "    for word in doc:\n",
    "        if word in doclist:\n",
    "            doc_v[doclist.index(word)] +=1\n",
    "    return doc_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "print(doc2V( data['docs'][0],docList))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def formDocs_v(docList):\n",
    "    docs_v = []\n",
    "    for doc in data['docs']:\n",
    "        docs_v.append(doc2V(doc,docList))\n",
    "\n",
    "    return docs_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "docs_v = list(map(lambda doc: doc2V(doc,docList),data['docs']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1]]\n"
     ]
    }
   ],
   "source": [
    "print(docs_v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''贝叶斯训练 \n",
    "   输入：docs_v: docs的向量矩阵  labels：标签\n",
    "   输出：p(Doc|C1),p(Doc|C0),p(C1)\n",
    "'''\n",
    "\n",
    "def train(docs_v,labels,docList):\n",
    "    #Abusive\n",
    "    n_doc = len(labels)\n",
    "    #向量基 永远是 docList\n",
    "    docLen = len(docList)\n",
    "    p1num = np.ones(docLen)\n",
    "    p0num = np.ones(docLen)\n",
    "    p1Denom,p0Denom =2,2\n",
    "\n",
    "    for i in range(n_doc):\n",
    "        if labels[i]==1:\n",
    "            p1num += docs_v[i]\n",
    "            p1Denom += np.sum(docs_v[i])\n",
    "        elif labels[i]==0:\n",
    "            p0num += docs_v[i]\n",
    "            p0Denom += np.sum(docs_v[i])    \n",
    "\n",
    "    return np.log(p1num/p1Denom),np.log(p0num/p0Denom),np.sum(labels)/n_doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1doc,p0doc,pA = train(docs_v,data['labels'],docList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def classify(doc,p1doc,p0doc,pA):\n",
    "    doc_v = doc2V(doc,docList)\n",
    "    p1 =np.sum(doc_v * p1doc) + np.log(pA)\n",
    "    p0 =np.sum(doc_v * p0doc) + np.log(1-pA)\n",
    "\n",
    "    if p1>p0:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classify('love my dalmation'.split(' '),p1doc,p0doc,pA)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a6dc62afd8b03c17538a9dfce2fcb18f62cec380cc7b77050462a64b7e4e4814"
  },
  "kernelspec": {
   "display_name": "Python 3.8.0 32-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
